package edt;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.border.TitledBorder;

import org.jetbrains.annotations.NotNull;
import org.joda.time.LocalDate;
import org.joda.time.LocalTime;
import org.joda.time.Period;
import org.joda.time.format.PeriodFormat;

import com.thirdnf.ResourceScheduler.Appointment;
import com.thirdnf.ResourceScheduler.Scheduler;

import exemple.AppointmentListener;
import exemple.ExampleComponentFactory;

public class MyScheduler extends JFrame {

	
    /**
	 * 
	 */
	private static final long serialVersionUID = -7623440689878590331L;
	MySchedulerModel model;
	JTextPane detailsPane;
	
	JTextField login;
	JPasswordField pass;
	CreneauModele currentAppointment;
	
	public MyScheduler()
    {
		
		currentAppointment = new CreneauModele("NuLL", new LocalTime());
		
        setSize(new Dimension(400, 400));

        Scheduler scheduler = new Scheduler();
        model = new MySchedulerModel();
        scheduler.setModel(model);
        scheduler.showDate(new LocalDate());
        
        ExampleComponentFactory componentFactory = new ExampleComponentFactory();
        componentFactory.setAppointmentListener(new AppointmentListener() {
            @Override
            public void handleClick(@NotNull Appointment appointment, int clickCount)
            {
                if (clickCount == 1) {
                    handleAppointmentClick((CreneauModele)appointment);
                }
            }
        });
        
        scheduler.setComponentFactory(componentFactory);
        
        
	    this.setSize(800, 400);
	    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    this.setLocationRelativeTo(null);
        
	    this.setLayout(new GridLayout(1, 2));
	    
	    
	    this.add(scheduler);
        
	    
	    JPanel colonneD = new JPanel();
	    colonneD.setLayout(new GridLayout(3, 1));
	    
	    this.add(colonneD);
	    
	    
  
	    
	    
        
        JButton add = new JButton("Ajouter");
        JButton mod = new JButton("Modifier");
        JButton remove = new JButton("Supprimer");
        
        JButton sync = new JButton("Synchro");
        JButton importButton = new JButton("Visualiser");
        
        
        add.addActionListener(new AddListener(this, model));
        
        mod.addActionListener(new EditButtonListener(this,model));
        
        remove.addActionListener(new RemoveListener(this,model));
        
        sync.addActionListener(new SyncListener(this,model));
        
        importButton.addActionListener(new ViewListener(this,model));
       
        detailsPane = new JTextPane();
        detailsPane.setBorder(new TitledBorder("Appointment Details"));
        
        
        JScrollPane scrollPane1 = new JScrollPane();
        
        scrollPane1.setMinimumSize(new Dimension(100, 100));
        
        scrollPane1.setViewportView(detailsPane);
        
       
        
        scrollPane1.setSize(300, 300);
	    colonneD.add(scrollPane1);
	    
	    
	    JPanel ligne2 = new JPanel();
	    ligne2.setLayout(new FlowLayout());       
	    
	    JPanel ligne3 = new JPanel();
	    ligne3.setLayout(new FlowLayout());   
        
	    
	    login = new JTextField(10);
	    pass = new JPasswordField(10);

        ligne2.add(add);
        ligne2.add(mod);
        ligne2.add(remove);
        ligne2.add(sync);
        ligne2.add(importButton);
	    
        
        
        JLabel loginLabel = new JLabel("Login :");
        ligne3.add(loginLabel);
        ligne3.add(login);
        JLabel mdpLabel = new JLabel("Mot de passe :");
        ligne3.add(mdpLabel);
        ligne3.add(pass);
        
        
        colonneD.add(ligne2);
        colonneD.add(ligne3);

        

       
        
    }


	
	
	
    /**
     * Affiche des informations sur le creneau selectionne quand on clic dessus
     *
     * @param appointment le creneau selectionne
     */
    private void handleAppointmentClick(CreneauModele appointment)
    {
    	
    	currentAppointment = appointment;
    	
    	
        StringBuilder stringBuilder = new StringBuilder();
        LocalTime time = appointment.getDateTime().toLocalTime();
        Period period = appointment.getDuration().toPeriod();

        stringBuilder.append("Matiere: ").append(appointment.getTitle()).append('\n')
                .append("Debut: ").append(time.toString("h:mm a")).append('\n')
                .append("Duree: ").append(period.toString(PeriodFormat.getDefault())).append('\n');
        
        stringBuilder.append("Type: ").append(appointment.getType()).append('\n');
        
        stringBuilder.append("Classe: ").append(appointment.getClasse()).append('\n');
        
        stringBuilder.append("Groupe: ").append(appointment.getGroupe()).append('\n');
                
        
        stringBuilder.append("Jour: ").append(appointment.getResource().getTitle());

        detailsPane.setText(stringBuilder.toString());
        
    }
	
	
	
	
	
}
